import 'dart:math';

class DataUtils {
  static int hexToInt(String hex) {
    int val = 0;
    int len = hex.length;
    for (int i = 0; i < len; i++) {
      int hexDigit = hex.codeUnitAt(i);
      if (hexDigit >= 48 && hexDigit <= 57) {
        val += (hexDigit - 48) * (1 << (4 * (len - 1 - i)));
      } else if (hexDigit >= 65 && hexDigit <= 70) {
        val += (hexDigit - 55) * (1 << (4 * (len - 1 - i)));
      } else if (hexDigit >= 97 && hexDigit <= 102) {
        val += (hexDigit - 87) * (1 << (4 * (len - 1 - i)));
      } else {
        throw new FormatException("Invalid hexadecimal value");
      }
    }
    return val;
  }

  ///二进制转10进制
  static int binToInt(String bin) {
    int len = bin.length;
    int value = 0;
    for (int i = 0; i < len; i++) {
      value += int.parse(bin[i]) * pow(2, len - 1 - i);
    }
    print("二进制：$bin 转十进制-->$value");
    return value;
  }

  ///八进制转10进制
  static int octalToInt(String bin) {
    int len = bin.length;
    int value = 0;
    for (int i = 0; i < len; i++) {
      value += int.parse(bin[i]) * pow(8, len - 1 - i);
    }
    print("八进制：$bin 转十进制-->$value");
    return value;
  }

  static int getRandomPort() {
    int value = 65535 - 1024;
    return Random().nextInt(value) + 1024;
  }
}
